<template>
  <a-modal
    :title="title"
    :width="1000"
    :visible="visible"
    :confirmLoading="confirmLoading"
    :maskClosable="false"
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭">

    <a-spin :spinning="confirmLoading">
      <a-form-model ref="form" :model="model" :rules="validatorRules">
        <a-row :gutter="24">
          <a-col :md="12" :sm="8">
            <a-form-model-item
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              prop="url"
              label="数据库连接">
              <a-input placeholder="请输入数据库连接" v-model="model.url"/>
            </a-form-model-item>
          </a-col>
          <a-col :md="12" :sm="8">
            <a-form-model-item
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              prop="username"
              label="用户名">
              <a-input placeholder="请输入用户名" v-model="model.username"/>
            </a-form-model-item>
          </a-col>
          <a-col :md="12" :sm="8">
            <a-form-model-item
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              prop="password"
              label="密码">
              <a-input placeholder="请输入密码" v-model="model.password"/>
            </a-form-model-item>
          </a-col>
          <a-col :md="12" :sm="8">
            <a-form-model-item
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              prop="driverClass"
              label="驱动类">
              <a-input placeholder="请输入驱动类" v-model="model.driverClass"/>
            </a-form-model-item>
          </a-col>
          <a-col :md="12" :sm="8">
            <a-form-model-item
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              prop="name"
              label="名称">
              <a-input placeholder="请输入名称" v-model="model.name"/>
            </a-form-model-item>
          </a-col>
          <a-col :md="12" :sm="8">
            <a-form-model-item
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              prop="databaseType"
              label="数据源类型">
              <j-dict-select-tag placeholder="请输入数据源类型" v-model="model.databaseType" dict-code="database_type" ></j-dict-select-tag>
            </a-form-model-item>
          </a-col>
        </a-row>

      </a-form-model>
    </a-spin>
  </a-modal>
</template>

<script>
import { httpAction } from '@/api/manage'
import pick from 'lodash.pick'
import validator from '@/utils/validate'

export default {
  name: 'SysDatabaseConfigModal',
  data() {
    return {
      title: '操作',
      visible: false,
      model: {},
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 },
      },
      input_width: { width: '270px' },
      confirmLoading: false,
      form: this.$form.createForm(this),
      validatorRules: {
        url: [
              { required: true, message: '请输入数据库连接!' },
              { max: 200, message: '长度不能大于200', trigger: 'blur' }],
        username: [
              { required: true, message: '请输入用户名!' },
              { max: 64, message: '长度不能大于64', trigger: 'blur' }],
        password: [
              { required: true, message: '请输入用户密码!' },
              { max: 200, message: '长度不能大于200', trigger: 'blur' }],
        driverClass: [
              { required: true, message: '请输入驱动类!' },
              { max: 200, message: '长度不能大于200', trigger: 'blur' }],
        name: [
              { required: true, message: '请输入名称!' },
              { max: 64, message: '长度不能大于64', trigger: 'blur' }],
        databaseType: [{required: true, message: '请选择数据源类型!'} ]

      },
      url: {
        add: '/generator/SysDatabaseConfig/add',
        edit: '/generator/SysDatabaseConfig/edit',
      }
      ,
    }
  },
  created() {
  },
  methods: {
    add() {
      this.edit({})
    },
    edit(record) {
      this.visible = true
      this.$nextTick(() => {
        this.$refs.form.resetFields()
        this.model = Object.assign({}, record);
      })
    },
    close() {
      this.$emit('close')
      this.visible = false
    },
    handleOk() {
      const that = this
      // 触发表单验证
      this.$refs.form.validate(valid => {
        if (valid) {
          that.confirmLoading = true
          let httpurl = ''
          let method = ''
          if (!this.model.id) {
            httpurl += this.url.add
            method = 'post'
          } else {
            httpurl += this.url.edit
            method = 'put'
          }
          httpAction(httpurl, this.model, method).then((res) => {
            if (res.success) {
              that.$message.success(res.message)
              that.$emit('ok')
            } else {
              that.$message.warning(res.message)
            }
          }).finally(() => {
            that.confirmLoading = false
            that.close()
          })
        }
      })
    },
    handleCancel() {
      this.close()
    },
  }
}
</script>

<style lang="less" scoped>

</style>
